<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" inline :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="RTUid" prop="rtuId">
      <el-input v-model="dataForm.rtuId" placeholder="RTUid"></el-input>
    </el-form-item>
    <el-form-item label="田块id" prop="farmlandInfoId">
      <el-input v-model="dataForm.farmlandInfoId" placeholder="田块id"></el-input>
    </el-form-item>
    <el-form-item label="部门（基地）id" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="部门（基地）id"></el-input>
    </el-form-item>
    <el-form-item label="水温" prop="waterTemperature">
      <el-input v-model="dataForm.waterTemperature" placeholder="水温"></el-input>
    </el-form-item>
    <el-form-item label="水深" prop="waterDepth">
      <el-input v-model="dataForm.waterDepth" placeholder="水深"></el-input>
    </el-form-item>
    <el-form-item label="风向" prop="windDirection">
      <el-input v-model="dataForm.windDirection" placeholder="风向"></el-input>
    </el-form-item>
    <el-form-item label="闸门1" prop="gateA">
      <el-input v-model="dataForm.gateA" placeholder="闸门1"></el-input>
    </el-form-item>
    <el-form-item label="闸门2" prop="gateB">
      <el-input v-model="dataForm.gateB" placeholder="闸门2"></el-input>
    </el-form-item>
    <el-form-item label="闸门3" prop="gateC">
      <el-input v-model="dataForm.gateC" placeholder="闸门3"></el-input>
    </el-form-item>
    <el-form-item label="闸门4" prop="gateD">
      <el-input v-model="dataForm.gateD" placeholder="闸门4"></el-input>
    </el-form-item>
    <el-form-item label="太阳能电池" prop="solarCell">
      <el-input v-model="dataForm.solarCell" placeholder="太阳能电池"></el-input>
    </el-form-item>
    <el-form-item label="预留1" prop="yuliuA">
      <el-input v-model="dataForm.yuliuA" placeholder="预留1"></el-input>
    </el-form-item>
    <el-form-item label="预留2" prop="yuliuB">
      <el-input v-model="dataForm.yuliuB" placeholder="预留2"></el-input>
    </el-form-item>
    <el-form-item label="闸门5" prop="gateE">
      <el-input v-model="dataForm.gateE" placeholder="闸门5"></el-input>
    </el-form-item>
    <el-form-item label="风速" prop="windVelocity">
      <el-input v-model="dataForm.windVelocity" placeholder="风速"></el-input>
    </el-form-item>
    <el-form-item label="原报文" prop="messageCode">
      <el-input v-model="dataForm.messageCode" placeholder="原报文"></el-input>
    </el-form-item>
    <el-form-item label="解析报文" prop="messageContent">
      <el-input v-model="dataForm.messageContent" placeholder="解析报文"></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createId">
      <el-input v-model="dataForm.createId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="创建用户姓名" prop="createName">
      <el-input v-model="dataForm.createName" placeholder="创建用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人id" prop="updateId">
      <el-input v-model="dataForm.updateId" placeholder="修改人id"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="修改用户姓名" prop="updateName">
      <el-input v-model="dataForm.updateName" placeholder="修改用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="删除标识 -1：删除 0：正常" prop="gcFlag">
      <el-input v-model="dataForm.gcFlag" placeholder="删除标识 -1：删除 0：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          rtuId: '',
          farmlandInfoId: '',
          deptId: '',
          waterTemperature: '',
          waterDepth: '',
          windDirection: '',
          gateA: '',
          gateB: '',
          gateC: '',
          gateD: '',
          solarCell: '',
          yuliuA: '',
          yuliuB: '',
          gateE: '',
          windVelocity: '',
          messageCode: '',
          messageContent: '',
          createId: '',
          createName: '',
          createTime: '',
          updateId: '',
          updateTime: '',
          updateName: '',
          version: '',
          gcFlag: ''
        },
        dataRule: {
          rtuId: [
            { required: true, message: 'RTUid不能为空', trigger: 'blur' }
          ],
          farmlandInfoId: [
            { required: true, message: '田块id不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '部门（基地）id不能为空', trigger: 'blur' }
          ],
          waterTemperature: [
            { required: true, message: '水温不能为空', trigger: 'blur' }
          ],
          waterDepth: [
            { required: true, message: '水深不能为空', trigger: 'blur' }
          ],
          windDirection: [
            { required: true, message: '风向不能为空', trigger: 'blur' }
          ],
          gateA: [
            { required: true, message: '闸门1不能为空', trigger: 'blur' }
          ],
          gateB: [
            { required: true, message: '闸门2不能为空', trigger: 'blur' }
          ],
          gateC: [
            { required: true, message: '闸门3不能为空', trigger: 'blur' }
          ],
          gateD: [
            { required: true, message: '闸门4不能为空', trigger: 'blur' }
          ],
          solarCell: [
            { required: true, message: '太阳能电池不能为空', trigger: 'blur' }
          ],
          yuliuA: [
            { required: true, message: '预留1不能为空', trigger: 'blur' }
          ],
          yuliuB: [
            { required: true, message: '预留2不能为空', trigger: 'blur' }
          ],
          gateE: [
            { required: true, message: '闸门5不能为空', trigger: 'blur' }
          ],
          windVelocity: [
            { required: true, message: '风速不能为空', trigger: 'blur' }
          ],
          messageCode: [
            { required: true, message: '原报文不能为空', trigger: 'blur' }
          ],
          messageContent: [
            { required: true, message: '解析报文不能为空', trigger: 'blur' }
          ],
          createId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createName: [
            { required: true, message: '创建用户姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          updateName: [
            { required: true, message: '修改用户姓名不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          gcFlag: [
            { required: true, message: '删除标识 -1：删除 0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrturecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rtuId = data.bussrturecord.rtuId
                this.dataForm.farmlandInfoId = data.bussrturecord.farmlandInfoId
                this.dataForm.deptId = data.bussrturecord.deptId
                this.dataForm.waterTemperature = data.bussrturecord.waterTemperature
                this.dataForm.waterDepth = data.bussrturecord.waterDepth
                this.dataForm.windDirection = data.bussrturecord.windDirection
                this.dataForm.gateA = data.bussrturecord.gateA
                this.dataForm.gateB = data.bussrturecord.gateB
                this.dataForm.gateC = data.bussrturecord.gateC
                this.dataForm.gateD = data.bussrturecord.gateD
                this.dataForm.solarCell = data.bussrturecord.solarCell
                this.dataForm.yuliuA = data.bussrturecord.yuliuA
                this.dataForm.yuliuB = data.bussrturecord.yuliuB
                this.dataForm.gateE = data.bussrturecord.gateE
                this.dataForm.windVelocity = data.bussrturecord.windVelocity
                this.dataForm.messageCode = data.bussrturecord.messageCode
                this.dataForm.messageContent = data.bussrturecord.messageContent
                this.dataForm.createId = data.bussrturecord.createId
                this.dataForm.createName = data.bussrturecord.createName
                this.dataForm.createTime = data.bussrturecord.createTime
                this.dataForm.updateId = data.bussrturecord.updateId
                this.dataForm.updateTime = data.bussrturecord.updateTime
                this.dataForm.updateName = data.bussrturecord.updateName
                this.dataForm.version = data.bussrturecord.version
                this.dataForm.gcFlag = data.bussrturecord.gcFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrturecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'rtuId': this.dataForm.rtuId,
                'farmlandInfoId': this.dataForm.farmlandInfoId,
                'deptId': this.dataForm.deptId,
                'waterTemperature': this.dataForm.waterTemperature,
                'waterDepth': this.dataForm.waterDepth,
                'windDirection': this.dataForm.windDirection,
                'gateA': this.dataForm.gateA,
                'gateB': this.dataForm.gateB,
                'gateC': this.dataForm.gateC,
                'gateD': this.dataForm.gateD,
                'solarCell': this.dataForm.solarCell,
                'yuliuA': this.dataForm.yuliuA,
                'yuliuB': this.dataForm.yuliuB,
                'gateE': this.dataForm.gateE,
                'windVelocity': this.dataForm.windVelocity,
                'messageCode': this.dataForm.messageCode,
                'messageContent': this.dataForm.messageContent,
                'createId': this.dataForm.createId,
                'createName': this.dataForm.createName,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'updateName': this.dataForm.updateName,
                'version': this.dataForm.version,
                'gcFlag': this.dataForm.gcFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
